home *** CD-ROM | disk | FTP | other *** search
/ ASME's Mechanical Engine…ing Toolkit 1997 December / ASME's Mechanical Engineering Toolkit 1997 December.iso / ai / prlg195b.lzh / GAMES.LZH / HANOI.PRO < prev    next >
Text File  |  1986-09-29  |  3KB  |  109 lines

  1. /* The driver "ansi.sys" must be in your config.sys file and on the boot
  2. disk for this to work. To start, type "start<CR>". */
  3.  
  4.  
  5.  
  6. putchar(X) :-  put(X).
  7.  
  8. makelist(1,[1]).
  9. makelist(N, [N|Y]) :- N1 is N - 1, makelist(N1,Y).
  10.  
  11. biggie(1,X,[X]).
  12. biggie(N,X,[X|Z]) :- N1 is N - 1, X1 is X + 1, biggie(N1,X1,Z).
  13.  
  14. alist(N,Y) :- biggie(N,1,Y).
  15.  
  16. /* get the size of a list  */
  17.  
  18. size([],0) :-  !.
  19. size([_|X],Num) :- size(X,N1), Num is N1 + 1.
  20.  
  21. /* Might as well keep track of the disks on the poles.  This is
  22.     not really necessary; all we need to know is how many
  23.     disks are on a pole  */
  24.  
  25.  
  26.  
  27. readtop(N,Y)  :- retract(pole(N,[Y|X])), asserta(pole(N,X)).
  28.  
  29. writetop(N,Y) :- retract(pole(N,X)),  asserta(pole(N,[Y|X])).
  30.  
  31. makepoles(N) :- alist(N,Y), asserta( pole(1,Y)),
  32.                 asserta(pole(2,[])), asserta(pole(3,[])).
  33.  
  34. /* stuff for pretty printing   */
  35. /* Note: the CONFIG.SYS file must contain the line ANSI.SYS.  Also,
  36.    the ANSI.SYS file must be on the disk when the system is booted     */
  37.  
  38. out(X) :- putchar(27), print(X).
  39. clear  :- out('[2J'). /* clear screen */
  40.  
  41. goto(X,Y)  :- putchar(27),print('[',X),putchar(59),print(Y,'H').   
  42. /* 59 is ; */
  43.  
  44.  
  45. stuff(1,X) :- print(X), !.
  46. stuff(N,X) :- print(X), N1 is N - 1, stuff(N1,X).
  47.  
  48.  
  49.  
  50. newhanoi(1,A,B,C) :- move(1,A,B).
  51. newhanoi(N,A,B,C) :- !,  N1 is N - 1,
  52.                        newhanoi(N1,A,C,B),
  53.                        move(N,A,B),
  54.                        newhanoi(N1,C,B,A).
  55.  
  56.  
  57.  
  58. /*  As mentioned earlier size and readtop are not really needed,
  59.     but I threw them in so that you can see what's there.             */
  60.  
  61. move(N,A,B)  :- !,   pole(A,Adisk), size(Adisk,ANum),readtop(A,N),
  62.                    X1 is 20 - ANum, Y1 is 5 + (A - 1)* 15,
  63.                    goto(X1,Y1), stuff(N,' '),
  64.                    writetop(B,N), pole(B,Bdisk), size(Bdisk,BNum),
  65.                    X2 is 20 - BNum, Y2 is 5 + (B - 1)* 15,
  66.                    goto(X2,Y2), stuff(N,'*'),
  67.                    goto(24,1),
  68.                    print('Move disk ',N,' from ',A,' to ',B,'         ').
  69.  
  70.  
  71. firstpole(N,1)  :- X1 is 20 - N, goto(X1,5),
  72.                    stuff(1,'*'), !.
  73.  
  74. firstpole(N,L) :-  X1 is (20 - N) + (L - 1), goto(X1,5),
  75.                    stuff(L,'*'),
  76.                    L1 is L - 1, firstpole(N,L1).
  77.  
  78.  
  79.  
  80. start :- print('How many disks? '), read(N), clear, firstpole(N,N),
  81.             makepoles(N), newhanoi(N,1,2,3), !.
  82.  
  83.  
  84.  
  85.  
  86. factor(0,Y) :- Y is 1, !.
  87. factor(X,Y) :- Z is X - 1, factor(Z,W), Y is X*W.
  88.  
  89. /* recursive version a n! and towers of hanoi_mov  */
  90. hanoi_mov(1,A,B,C) :- print('Move disk ',1,' from ',A,' to ',B),nl, !.
  91. hanoi_mov(N,A,B,C) :-   N1 is N - 1,
  92.                     hanoi_mov(N1,A,C,B), !,
  93.                     print('Move disk ',N,' from ',A,' to ',B), nl,
  94.                     hanoi_mov(N1,C,B,A), !.
  95.  
  96.  
  97.  
  98.  
  99.  
  100.  
  101.  
  102.  
  103.  
  104.  
  105.  
  106.  
  107.  
  108.  
  109.